﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.IO;

namespace eLearning.Utility
{
    public static class LeadTools
    {
        #region Date/Time
        public static string GetDayText(string date)
        {
            DayOfWeek dw = DateTime.Parse(date).DayOfWeek;
            string dt = string.Empty;

            switch (dw)
            {
                case DayOfWeek.Sunday:
                    {
                        dt = "Chủ nhật";
                        break;
                    }
                case DayOfWeek.Monday:
                    {
                        dt = "Thứ hai";
                        break;
                    }
                case DayOfWeek.Tuesday:
                    {
                        dt = "Thứ ba";
                        break;
                    }
                case DayOfWeek.Wednesday:
                    {
                        dt = "Thứ tư";
                        break;
                    }
                case DayOfWeek.Thursday:
                    {
                        dt = "Thứ năm";
                        break;
                    }
                case DayOfWeek.Friday:
                    {
                        dt = "Thứ sáu";
                        break;
                    }
                case DayOfWeek.Saturday:
                    {
                        dt = "Thứ bảy";
                        break;
                    }
            }

            return string.Format("{0}", dt);
        }

        public static string GetTodayText()
        {
            DayOfWeek dw = DateTime.Now.DayOfWeek;
            string dt = string.Empty;

            switch (dw)
            {
                case DayOfWeek.Sunday:
                    {
                        dt = "Chủ nhật";
                        break;
                    }
                case DayOfWeek.Monday:
                    {
                        dt = "Thứ hai";
                        break;
                    }
                case DayOfWeek.Tuesday:
                    {
                        dt = "Thứ ba";
                        break;
                    }
                case DayOfWeek.Wednesday:
                    {
                        dt = "Thứ tư";
                        break;
                    }
                case DayOfWeek.Thursday:
                    {
                        dt = "Thứ năm";
                        break;
                    }
                case DayOfWeek.Friday:
                    {
                        dt = "Thứ sáu";
                        break;
                    }
                case DayOfWeek.Saturday:
                    {
                        dt = "Thứ bảy";
                        break;
                    }
            }

            return string.Format("{0}, {1}", dt, DateTime.Now.ToString("dd-MM-yyyy"));
        }
        public static DateTime GetFirstDayOfMonth(int iMonth, int iYear)
        {
            DateTime dtFrom = new DateTime(iYear, iMonth, 1);
            dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));

            return dtFrom;
        }

        public static DateTime GetFirstDayOfMonth(int iMonth)
        {
            DateTime dtFrom = new DateTime(DateTime.Now.Year, iMonth, 1);
            dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));

            return dtFrom;
        }

        public static DateTime GetLastDayOfMonth(DateTime dtDate)
        {
            DateTime dtTo = dtDate;

            dtTo = dtTo.AddMonths(1);
            dtTo = dtTo.AddDays(-(dtTo.Day));

            return dtTo;
        }

        public static DateTime EndOfDay(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, date.Day, 23, 59, 59, 999);
        }

        public static DateTime StartOfDay(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, date.Day, 1, 1, 0, 0);
        }


        #endregion

        #region XML
        // Read data from xml
        public static T Deserialize<T>(string filePath, T data)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(T));

            try
            {
                TextReader textReader = new StreamReader(filePath);
                data = (T)deserializer.Deserialize(textReader);
                textReader.Close();
            }
            catch
            {
            }
            return data;
        }

        public static void Serializer<T>(string filePath, T data)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));

            try
            {
                TextWriter txtWriter = new StreamWriter(filePath);
                serializer.Serialize(txtWriter, data);
                txtWriter.Close();
            }
            catch
            {
            }
        }
        #endregion
    }
}